<template>
  <div class="container">
    <el-button
      size="small"
      type="primary"
      @click="addstandardConfiguration"
      style="margin-bottom: 20px; float: right"
      >添加标准配置<i class="el-icon-circle-plus-outline"></i
    ></el-button>
    <!-- v-loading="isLoading" -->

    <el-table
      :data="tableData"
      style="width: 100%"
      :row-class-name="xxxTableRowClassName"
      :header-cell-style="{
        background: 'rgba(61,157,255,0.05)',
        color: '#333333',
      }"
    >
      <el-table-column type="index" width="50">
         </el-table-column>
      <el-table-column
        prop="standardConfigurationName"
        label="标准名称"
        width="width"
        align="center"
      >
      </el-table-column>
  
      <el-table-column
        prop="userName"
        label="标准描述"
        width="width"
        align="center"
      >
      </el-table-column>
        <el-table-column
        prop="userName"
        label="适用范围"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="width" align="center">
        <template slot-scope="{ row }">
     
          <el-button
            type="danger"
            size="small"
            plain
            @click="delstandardConfiguration($event, row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="form.standardConfigurationId ? '修改标准配置' : '添加标准配置'"
      :visible.sync="dialogFormVisible"
      width="700px"
      :close-on-click-modal="false"
    >
      <el-form
        label-position="left"
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
      >
        <el-form-item label="姓名:" prop="standardConfigurationName">
          <el-input
            v-model="form.standardConfigurationName"
            style="width: 70%"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号:" prop="phone">
          <el-input v-model="form.phone" style="width: 70%"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button :loading="disable" size="small" type="primary" @click="save"
          >{{ disable ? "加载中" : "确 定" }}
        </el-button>
      </div>
    </el-dialog>

    <pagination-page @size="changeSize" @page="changePage" :total="total" />
  </div>
</template>

<script>
// import {
//   standardConfigurationList,
//   addstandardConfiguration,
//   sendTeachMessage,
//   delstandardConfiguration,
// } from "@/api/school/standardConfiguration/index.js";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      disable: false,
      total: -1,
      loading: false,
      page: {
        pageSize: 10,
        pageNum: 1,
      },
      dialogFormVisible: false,
      tableData: [],
      form: {
        standardConfigurationName: "",
        phone: "",
        pic: "",
        standardConfigurationId: undefined,
      },
      rules: {
        standardConfigurationName: [
          {
            required: true,
            message: "请输入名字",
          },
          {
            min: 2,
            max: 10,
            message: "长度必须在2-10个之间",
            trigger: "blur", // 触发校验的时机是失去焦点
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
          },
          {
            min: 7,
            max: 11,
            message: "长度必须在7-11个之间",
            trigger: "blur", // 触发校验的时机是失去焦点
          },
        ],
      },
      isLoading: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getstandardConfigurationList();
  },
  
  mounted() {},
  methods: {
    //获取标准配置列表
    getstandardConfigurationList() {
      let data = {
        userId: this.$store.state.user.USER_INFO.id,
        ...this.page,
        schoolId: this.$store.state.user.USER_INFO.pscShId,
      };
      this.isLoading = true;
      standardConfigurationList(data).then((res) => {
        if (res.code == 200) {
          this.tableData = res.result;
          this.total = res.total;
          this.isLoading = false;
        }
      });
    },
    //添加标准配置
    addstandardConfiguration() {
      this.dialogFormVisible = true;
      (this.form = {
        standardConfigurationName: "",
        phone: "",
        standardConfigurationId: "",
        pic: "",
      }),
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate();
        });
    },
    //编辑标准配置
    compile(evt, row) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.dialogFormVisible = true;
      this.form = { ...row };
      this.form.phone = Decrypt(this.form.phone);
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate();
      });
    },
    //删除标准配置
    delstandardConfiguration(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          let data = {
            standardConfigurationId: row.standardConfigurationId,
          };
          delstandardConfiguration(data).then((res) => {
            if (res.code === 200) {
              this.getstandardConfigurationList();
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();

          this.$message({
            type: "info",
            message: "取消删除",
          });
        });
    },
    save() {
      let data = {
        ...this.form,
        schoolId: this.$store.state.user.USER_INFO.pscShId,
        phone: encryption(this.form.phone),
      };

      // 对所有表单项进行统一校验
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.disable = true;

          addstandardConfiguration(data).then((res) => {
            if (res.code === 200) {
              this.dialogFormVisible = false;
              this.getstandardConfigurationList();
              this.$message.success("保存成功");
              this.disable = false;
            } else {
              this.disable = false;
              this.dialogFormVisible = false;
              this.$message.error(res.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    xxxTableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    //确定保存
    send(evt, row) {
      let target = evt.target;
      this.$confirm("是否确定发送?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          sendTeachMessage({
            standardConfigurationId: row.standardConfigurationId,
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message);
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          this.$message({
            type: "info",
            message: "取消发送",
          });
        });
    },
    changeSize(size) {
      this.page.pageSize = size;
      this.getstandardConfigurationList();
    },
    changePage(page) {
      this.page.pageNum = page;
      this.getstandardConfigurationList();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
::v-deep .el-upload-list__item {
  display: none;
}
::v-deep .el-form-item__label {
  padding-left: 40px;
}
</style>
